'''
Created on Aug 13, 2012

@author: petrina
'''

import datetime
import logging

class Agg_record(object):
    '''
    The class describes aggregated fleet records. 
    
    An instance of this class represent a single record of data for one link 
    in a certain direction at a specific timestamp (e.g. using 15 minute intervals).
    The class does not contain static Link-data such as length or frc.
     
    '''


    def __init__(self, speed = 0, coverage = 0, count = 0, 
                 datasource = 'unknown', timestamp = datetime.datetime.now(), link = None, 
                 interval=None, int_size = 15, is_kph = True, std = 0):
        '''

        @param speed: harmonic speed of the taxis or average speed
        @param coverage: percentage of taxis passing the link 
        @param count: number of mesurement
        @param datasource: used data (e.g. referee) 
        @param timestamp: timestamp at the beginning of the time-interval of the measurement (default: now)
        @param link: link-object (containing the static data of the link, such as its length)
        @param interval: time interval (default: None, i.e. it will be calculated)
        @param int_size: length of the time interval in minutes (default: 15)  
        @param is_kph: True (=default): the speed's unit is kilometers per hour (kph or km/h), False: the speeds' unit is meters per second (m/s)  
        @param std: standard deviation for loading timeseries (defaul: 0)
        '''
 
        self.is_kph = is_kph
        self.speed = speed
        self.coverage = coverage
        self.count = count
        self.datasource = datasource
        self.timestamp = timestamp
        self.link = link
        self.interval = interval
        
        # for timeseries
        self.std = std
        
        # calculate interval based on 15 minutes intervals
        if interval is None:
            self.interval = (timestamp.hour * 60 + timestamp.minute)/int_size
        
        logging.debug("Agg_record: created new Agg_record " + self.__str__())



    #-----------------------getter- and setter-methods-------------------------


    def get_is_kph(self):
        return self.__is_kph


    def set_is_kph(self, value):
        self.__is_kph = value


    def del_is_kph(self):
        del self.__is_kph
        

    def get_std(self):
        return self.__std


    def get_speed_avg(self):
        return self.__speed + self.__std * self.__std/(self.__speed + 0.0)


    def set_std(self, value):
        self.__std = value


    def del_std(self):
        del self.__std


    def get_interval(self):
        return self.__interval


    def set_interval(self, value):
        self.__interval = value


    def del_interval(self):
        del self.__interval


    def get_timestamp(self):
        return self.__timestamp


    def get_link(self):
        return self.__link


    def set_timestamp(self, value):
        self.__timestamp = value


    def set_link(self, value):
        self.__link = value


    def del_timestamp(self):
        del self.__timestamp


    def del_link(self):
        del self.__link
   

    def get_speed(self):
        return self.__speed


    def get_traveltime(self):
        if self.__speed is not None and self.__speed > 0:

            if self.__is_kph:
                # assuming speed is in kilometers per hour
                return 3.6 * self.__link.length/self.__speed
            else:
                # assuming speed is in meters per second
                return self.__link.length/self.__speed
        else:
            logging.error("Agg_record: cannot calculate traveltime of this Agg_record " + self.__str__() )
            return None


    def get_coverage(self):
        return self.__coverage


    def get_count(self):
        return self.__count


    def get_datasource(self):
        return self.__datasource


    def set_speed(self, value):
        self.__speed = float(value)


    def set_coverage(self, value):
        self.__coverage = value


    def set_count(self, value):
        self.__count = value


    def set_datasource(self, value):
        self.__datasource = value


    def del_speed(self):
        del self.__speed


    def del_coverage(self):
        del self.__coverage


    def del_count(self):
        del self.__count


    def del_datasource(self):
        del self.__datasource
        
    
    def __str__(self):
        unit = 'km/h' if self.__is_kph else 'm/s'
        return str(self.__timestamp) + ": v=" + str(self.__speed) +  unit + ", count="+ str(self.__count) + "link=" + self.__link.__str__()    
        
        
    #--------------assign properties using get and set-methods-----------------
    
    speed = property(get_speed, set_speed, del_speed, "aggregated (harmonic) speed of the fcd at the given link and time-interval")
    traveltime = property(get_traveltime, None, None, "calculated travel time")
    coverage = property(get_coverage, set_coverage, del_coverage, "percentage of vehicles which fully passed the link")
    count = property(get_count, set_count, del_count, "number of vehicles that passed the link")
    datasource = property(get_datasource, set_datasource, del_datasource, "datasource of the fcd-data")
    timestamp = property(get_timestamp, set_timestamp, del_timestamp, "begin timestamp of the aggreated data")
    link = property(get_link, set_link, del_link, "link-object")
    interval = property(get_interval, set_interval, del_interval, "time interval (e.g. 0 is between 0:00 and 0:15")    
    std = property(get_std, set_std, del_std, "standard deviation")
    speed_avg = property(get_speed_avg, None, None, "speed_avg= speed + std_dev^2/speed")
    is_kph = property(get_is_kph, set_is_kph, del_is_kph, "the unit of the speeds is kilometers per hour (kph)")
